
Chapter B4. An Echoing Client/Server Application Using BlueCove

From:
  Killer Game Programming in Java 

  Dr. Andrew Davison
  Dept. of Computer Engineering
  Prince of Songkla University
  Hat Yai, Songkhla 90112, Thailand
  E-mail: ad@fivedots.coe.psu.ac.th

  Web Site for the book: http://fivedots.coe.psu.ac.th/~ad/jg


If you use this code, please mention my name, and include a link
to the book's Web site.

**NOTE**: this example is NOT in the hardcopy book printed by O'Reilly.

Thanks,
  Andrew


============================

This example is a client/server application using the
BlueCove implementation of the Java API for Bluetooth (JSR-82). 
BlueCove can be downloaded from http://bluecove.org/

I tested this code on several Windows XP machines, running either 
the Microsoft Bluetooth stack or the Widcomm/Broadcom stack. I recommend using
the default Microsoft stack, since it's less hassle to setup than the
Widcomm stack for beginners.

The server code is in the Server\ directory, the client code in Client\.

The simplest way to compile and run this code is to put copies of
the BlueCove JAR file (e.g. bluecove-2.1.0.jar) in Server\ *and* in
Client\.

============================

The Server-side (in Server\)

The Echo server consists of 2 Java files, and 2 batch files:
  *  EchoServer.java, ThreadedEchoHandler.java
  *  compile.bat, run.bat

Compilation and Execution:

> compile *.java
     // uses compile.bat; assumes that bluecove-2.1.0.jar is in the directory

> run EchoServer
     // uses run.bat; assumes that bluecove-2.1.0.jar is in the directory


============================

The Client-side (in Client\)

The Echo client consists of 4 Java files, and 2 batch files:
  *  EchoClient.java, TimeOutTimer.java, 
     DevicesFinder.java, ServiceFinder.java
  *  compile.bat, run.bat

Compilation and Execution:

> compile *.java
     // uses compile.bat; assumes that bluecove-2.1.0.jar is in the directory

> run EchoClient
     // uses run.bat; assumes that bluecove-2.1.0.jar is in the directory


============================

Client Notes

The main() function of EchoClient.java contains commented-out code that allows 
devices and services discovery to be carried out in different ways. e.g.

1. Full devices and services search (the default):

ServiceFinder srvFinder =  new ServiceFinder(ServiceFinder.RFCOMM, UUID_STRING, SERVICE_NAME);
ServiceRecord sr = srvFinder.find();
if (sr != null)
  new EchoClient(sr); 


2. Full devices and services search *without* a service UUID:

ServiceFinder srvFinder = new ServiceFinder(ServiceFinder.RFCOMM, SERVICE_NAME);
ServiceRecord sr = srvFinder.find();
if (sr != null)
  new EchoClient(sr); 


3. Fixed device (using its Bluetooth address) and services search:

ServiceFinder srvFinder =  new ServiceFinder(ServiceFinder.RFCOMM, UUID_STRING, SERVICE_NAME);
ServiceRecord sr = srvFinder.findOn(ECHO_BT_ADDRESS);
if (sr != null)
  new EchoClient(sr); 


4. Fixed device (using its Bluetooth address) and services search *without a service UUID:

ServiceFinder srvFinder =  new ServiceFinder(ServiceFinder.RFCOMM, SERVICE_NAME);
ServiceRecord sr = srvFinder.findOn(ECHO_BT_ADDRESS);
if (sr != null)
  new EchoClient(sr); 


---------
Last updated: 28th February 2011
